//
//  InlandViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "InlandViewController.h"
#import "WorldDetailViewController.h"
@interface InlandViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *inlandCV;
@end

@implementation InlandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.HKArray = [NSMutableArray array];
    self.mainlandArray = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(160 * WID, 230 * HEI);
    flowLayout.minimumLineSpacing = 20 * HEI;
    flowLayout.minimumInteritemSpacing = 0.5 * HEI;
    flowLayout.sectionInset = UIEdgeInsetsMake(20 * WID, 20 * WID, 20 * WID, 20 * WID);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.inlandCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 140 * HEI) collectionViewLayout:flowLayout];
    self.inlandCV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.inlandCV];
    
    self.inlandCV.delegate = self;
    self.inlandCV.dataSource = self;
    [self.inlandCV registerClass: [CollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.inlandCV registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"LJPCollectionViewCell"];
    
    

    [self getAFNetWorkingData];


}
-(void)getAFNetWorkingData{
    NSString *url_string = @"http://chanyouji.com/api/destinations.json";
    [LSAppTools getDataWithURL:url_string blcok:^(id result) {
        NSMutableArray *bigArray = [NSMutableArray arrayWithArray:result];
        for (NSMutableDictionary *bigDic in bigArray) {
            NSString *count = [bigDic objectForKey:@"category"];
            NSMutableArray *destinationsArray = [bigDic objectForKey:@"destinations"];
            
            
            for (NSMutableDictionary *dic in destinationsArray) {
                if ([count isEqualToString:@"99"]) {
                    ForeignModel *foreignModel = [[ForeignModel alloc] init];
                    [foreignModel setValuesForKeysWithDictionary:dic];
                    [self.HKArray addObject:foreignModel];
                }else if ([count isEqualToString:@"999"]){
                    ForeignModel *foreignModel = [[ForeignModel alloc] init];
                    [foreignModel setValuesForKeysWithDictionary:dic];
                    [self.mainlandArray addObject:foreignModel];
                }
                
            }
            [self.inlandCV reloadData];
            
        }

        
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (section == 0) {
        return self.HKArray.count;
    }else{
        return self.mainlandArray.count;
    }
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"LJPCollectionViewCell";
    
    CollectionViewCell *inlandCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    ForeignModel *foreignModel = [[ForeignModel alloc] init];
    if (indexPath.section == 0) {
        foreignModel = [self.HKArray objectAtIndex:indexPath.item];
    }else{
        foreignModel = [self.mainlandArray objectAtIndex:indexPath.item];
    }
    
    inlandCell.foreignModel = foreignModel;
    
    return inlandCell;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, 30 * HEI);
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        CollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            headView.label.text = @"港澳台";
        }else if (indexPath.section == 1){
            headView.label.text = @"大陆";
        }
        headView.label.textAlignment =  NSTextAlignmentCenter;
        return headView;
    }
    
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    WorldDetailViewController *worldDetailVC = [[WorldDetailViewController alloc] init];
    
    worldDetailVC.hidesBottomBarWhenPushed = YES;
    if (indexPath.section == 0) {
        worldDetailVC.foreignModel = [self.HKArray objectAtIndex:indexPath.row];
    }else if (indexPath.section == 1){
        worldDetailVC.foreignModel = [self.mainlandArray objectAtIndex:indexPath.row];
    }
    
    [self.navigationController pushViewController:worldDetailVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
